﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Transactions;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Core.Common;
using RHDOMnt = Texfina.DOMnt.Rh;
using RHDOQry = Texfina.DOQry.Rh;
using SYDOMnt = Texfina.DOMnt.Sy;
using Texfina.Entity.Sy;

namespace Texfina.BOMnt.Rh
{
    /// <summary>
    /// Business Object para Mantenimiento a la Tabla: Afp (RH_Afp)
    /// </summary>
    public class Afp : BusinessObjectBase, IBOUpdate
    {

        #region IBOUpdate Members

        public string[] UpdateData(IEntityBase value)
        {

            EAfpSet objE = (EAfpSet)value;
            object[] objRet = null;

            try
            {

                using (TransactionScope tx = new TransactionScope())
                {

                    this.DeleteDetail(objE.AfpComi, true);

                    //objRet = this.UpdateMaster(objE.Afp);
                    this.UpdateDetail(objE.AfpComi, objRet);

                    tx.Complete();

                }

                if (objRet == null)
                    return null;

                return new String[] { objRet[0].ToString() };

            }
            catch (Exception ex)
            {

                throw ex;

            }
        }

        public int DeleteData(IEntityBase value)
        {

            EAfpSet objE = (EAfpSet)value;

            try
            {

                using (TransactionScope tx = new TransactionScope())
                {

                    this.DeleteDetail(objE.AfpComi, false);
                    this.DeleteMaster(objE.Afp);

                    tx.Complete();

                }

                return 1;

            }
            catch (Exception ex)
            {

                throw ex;

            }

        }

        #endregion

        private object[] UpdateMaster(EAfp entity)
        {

            IDOUpdate objDO = new RHDOMnt.Afp();

            if (entity.EntityState == EntityState.Unchanged)
                return null;

            object[] objRet = null;

            switch (entity.EntityState)
            {

                case EntityState.Added:
                    objRet = objDO.Insert(entity);
                    break;

                case EntityState.Modified:
                    if (entity.StAnulado == ConstFlagEstado.DESACTIVADO)
                        objDO.Update(entity);
                    else
                        objDO.Delete(entity);
                    break;

                case EntityState.Deleted:
                    objDO.Delete(entity);
                    break;

            }

            return objRet;
        }

        private void UpdateDetail(IList<EAfpComi> col, object[] primKey)
        {

            if (col == null)
                return;
            if (col.Count == 0)
                return;

            IDOUpdate objDO = new RHDOMnt.AfpComi();
            //AUDIT
            IDOUpdate objDOAUD = new SYDOMnt.Auditoria();
            EAuditoria objAUD = new EAuditoria();
            //AUDIT END

            foreach (EAfpComi objE in col)
            {

                object[] objRet = null;

                if (primKey != null)
                {
                    objE.IdAfp = primKey[0].ToString();
                }

                switch (objE.EntityState)
                {

                    case EntityState.Added:
                        objRet = objDO.Insert(objE);
                        objAUD = EAudStatic.CreateEntity<EAfpComi>(ConstTipoAuditoria.NEW, objE.IdAfp + "-" + objE.IdPeriodo + "-" + objE.IdMes + "-" + objE.IdTipoComi, "", null, null, null);
                        break;

                    case EntityState.Modified:
                        if (objE.StAnulado == ConstFlagEstado.DESACTIVADO)
                        {
                            IDOQuery objDOQUERY = new RHDOQry.AfpComi();
                            EAfpComi ObjEOrig = (EAfpComi)objDOQUERY.GetByKey(objE);
                            objDO.Update(objE);
                            objAUD = EAudStatic.CreateEntity<EAfpComi>(ConstTipoAuditoria.EDIT, objE.IdAfp + "-" + objE.IdPeriodo + "-" + objE.IdMes + "-" + objE.IdTipoComi, "", ObjEOrig, objE, null);
                            if (objAUD.DsEvento == "")
                                continue;
                        }
                        else
                        {
                            objDO.Delete(objE);
                            objAUD = EAudStatic.CreateEntity<EAfpComi>(ConstTipoAuditoria.DELETE, objE.IdAfp + "-" + objE.IdPeriodo + "-" + objE.IdMes + "-" + objE.IdTipoComi, "", null, null, null);
                        }
                        break;

                }

                objDOAUD.Insert(objAUD);

            }

        }

        private void DeleteMaster(EAfp entity)
        {

            IDOUpdate objDO = new RHDOMnt.Afp();

            if (entity.EntityState == EntityState.Unchanged)
                return;

            objDO.Delete(entity);

        }

        private void DeleteDetail(IList<EAfpComi> col, bool filterModified)
        {

            if (col == null)
                return;
            if (col.Count == 0)
                return;

            IDOUpdate objDO = new RHDOMnt.AfpComi();
            IEnumerable<EAfpComi> colSel = null;

            if (filterModified)
            {
                var deleteItems = from itm in col
                                  where itm.EntityState == EntityState.Deleted
                                  select itm;

                colSel = (IEnumerable<EAfpComi>)deleteItems;
            }
            else
            {
                var deleteItems = from itm in col
                                  select itm;

                colSel = (IEnumerable<EAfpComi>)deleteItems;
            }
            string code = "";
            foreach (EAfpComi objE in colSel)
            {
                objDO.Delete(objE);
                code = (code == "" ? code : code + ",") + "[" + objE.IdPeriodo + "-" + objE.IdAfp + "-" + objE.IdMes + "-" + objE.IdTipoComi + "]";
            }

            //AUDIT
            if (code != "")
            {
                IDOUpdate objDOAUD = new SYDOMnt.Auditoria();
                EAuditoria objAUD = new EAuditoria();
                objAUD = EAudStatic.CreateEntity<EAfpComi>(ConstTipoAuditoria.DELETE, "", code, null, null, null);
                objDOAUD.Insert(objAUD);
            }
            //AUDIT END

        }

    }
}
